package com.cn.base.事务;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author: dlr
 * @data: 2019/8/18 23:28
 * @desc:消息生产者事务
 */
public class producerTration {
    private final static String MQ_URL="tcp://192.168.10.20:61616";
    //点对点对队列名
    private final static String QUEUE_NAME="BASE_QUEUE";

    public static void main(String[] args) throws Exception{
        //连接工厂
        ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory(MQ_URL);
        //获取连接
        Connection connection=factory.createConnection();
        //启动
        connection.start();
        //创建session
        //参数1:是否开启事务,参数2:签收模式
        Session session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        //创建消息目的地点对点队列
        Queue queue=session.createQueue(QUEUE_NAME);
        //创建消息的生产者
        MessageProducer producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        //生产三条消息向队列中
        for(int i=1;i<=3;i++){
            //创建消息
            TextMessage textMessage = session.createTextMessage("第" + i + "条消息");
            //发送到mq的队列中
            producer.send(textMessage);
        }
        System.err.println("success");
        session.commit();
        producer.close();
        session.close();
        connection.close();
    }
}
